﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using nwrole.lib;

namespace nwrole.lib2 {
    public class SupervisorHost {
        
        private nwrole.lib2.Supervisor supervisor;
        private ServiceHost serviceHost;

        private static void WriteLog(string content) {
            DateTime now = DateTime.Now;
            var path = "c:/log/nwrole/Supervisor/";
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);

            System.IO.File.AppendAllText(path + "log " + now.ToString("yyyy-MM-dd") + ".txt", now.ToString("yyyy-MM-dd hh:mm:ss ") + content + "\r\n");
        }

        public bool Start(int supervisorPort) {
            try {
                WriteLog("SupervisorHost Start 1");
                if (serviceHost != null)
                    serviceHost.Close();
                if (supervisor != null)
                    supervisor.Stop();
                WriteLog("SupervisorHost Start 2");
                Uri baseAddress = new Uri("net.tcp://localhost:" + supervisorPort);

                supervisor = new nwrole.lib2.Supervisor(supervisorPort);
                serviceHost = new ServiceHost(supervisor, baseAddress);
                NetTcpBinding binding = new NetTcpBinding();
                binding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
                binding.MaxReceivedMessageSize = 2147483647;
                binding.ReceiveTimeout = new TimeSpan(0, 0, 120);
                serviceHost.Credentials.WindowsAuthentication.AllowAnonymousLogons = true;
                serviceHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Windows;
                serviceHost.AddServiceEndpoint(typeof(ISupervisorContract), binding, "Supervisor");
                serviceHost.Faulted += new EventHandler(serviceHost_Faulted);
                serviceHost.Opening += new EventHandler(serviceHost_Opening);
                serviceHost.Opened += new EventHandler(serviceHost_Opened);
                serviceHost.Open();

                WriteLog("SupervisorHost Start 3");

                supervisor.Start();
                WriteLog("SupervisorHost Start 4");
            } catch (Exception exception) {
                WriteLog("SupervisorHost Start exception: "+ exception);
                EventLogger.LogException("SupervisorHost exception: ", exception);
                return false;
            }

            return true;
        }

        public void Stop() {
            if (serviceHost != null) {
                supervisor.Stop();
                serviceHost.Close();
                serviceHost = null;
                supervisor = null;
            }
        }

        private void serviceHost_Opened(object sender, EventArgs e) {
        }

        private void serviceHost_Opening(object sender, EventArgs e) {
        }

        private void serviceHost_Faulted(object sender, EventArgs e) {
            StringBuilder sb = new StringBuilder();
            if (sender != null) {
                sb.Append(sender.ToString());
            }
            if (e != null) {
                sb.Append(e.ToString());
            }

            EventLogger.LogInformation("SupervisorHost faulted: " + sb.ToString());
        }

        
    }
}
